package abc;

import abc.generator.FastAbcGenerator;
import abc.utils.StringUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.FileNotFoundException;

//这里定义了插件目标名称以及其他一些配置
@Mojo(name = "abc-generate", requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME,
        requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class AbcMojo extends AbstractMojo {

    //这里注入插件的配置信息
    @Parameter(property = "microservice-sdk.projectName", defaultValue = "${project.name}")
    protected String projectName;

    @Parameter(property = "microservice-sdk.supportNacos", defaultValue = "false")
    protected String supportNacos;

    @Parameter(property = "microservice-sdk.excludeFields", defaultValue = "")
    protected String excludeFields;

    @Parameter(property = "microservice-sdk.ignoreAssociative", defaultValue = "")
    protected String ignoreAssociative;

    @Parameter(property = "microservice-sdk.logicDeleteField", defaultValue = "")
    protected String logicDeleteField;

    @Parameter(property = "microservice-sdk.versionField", defaultValue = "")
    protected String versionField;

    @Parameter(property = "microservice-sdk.context", defaultValue = "/")
    protected String context;

    @Parameter(property = "microservice-sdk.apiInputParameterType", defaultValue = "")
    protected String apiInputParameterType;

    //这里注入插件的配置信息
    @Parameter(property = "microservice-sdk.mode", defaultValue = "")
    protected String mode;

    @Parameter(property = "microservice-sdk.swagger", defaultValue = "false")
    protected String swagger;

    @Parameter(property = "microservice-sdk.application", defaultValue = "service-abc")
    protected String application;

    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("准备生成代码到" + projectName);
        try {
            if(StringUtil.isEmpty(context)) {
                context = "";
            }
            FastAbcGenerator.main(new String[] {mode, projectName, supportNacos, context, excludeFields, logicDeleteField, versionField, ignoreAssociative, apiInputParameterType, swagger, application});
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
